package com.freemypay.java.十二通过异常处理错误.十二其他可选方式.四被检查异常转换为不检查异常;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * "Turning off" checked exception 
 * @author yesixian
 * @date 2018年7月27日
 */
public class TurnOffChecking {
	public static void main(String[] args) {
		WrapCheckException wce = new WrapCheckException();
		// You can call throwRuntimeException() without a try
		// block, and let RuntimeException leave the method;
		wce.throwRuntimeException(3);
		// Or you can choose to catch exception
		for (int i = 0; i < 4; i++) {
			try {
				if(i < 3) {
					wce.throwRuntimeException(i);
				}else {
					throw new SomeOtherException();
				}
			} catch (SomeOtherException e) {
				// TODO: handle exception
				System.out.println("SomeOtherException: " + e);
			} catch (RuntimeException re) {
				// TODO: handle exception
				try {
					throw re.getCause();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					System.out.println("FileNotFoundException: " + e);
				} catch (IOException e) {
					// TODO: handle exception
					System.out.println("IOException: " + e);
				} catch (Throwable e) {
					// TODO: handle exception
					System.out.println("Throwable: " + e);
				}
			}
			
		}

	}
}

class WrapCheckException {
	void throwRuntimeException(int type) {
		try {
			switch (type) {
			case 0: throw new FileNotFoundException();
			case 1: throw new IOException();
			case 2: throw new RuntimeException("Where am I?");
			default: return;
			}
		} catch (Exception e) { // Adapt to unchecked;
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}
}

class SomeOtherException extends Exception {}
